package com.android.hesapci;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.HashMap;

public class Hesapci2 extends Activity
{
    private Spinner usersSpinner;
    ArrayAdapter<String> usersSpinnerAdapter;
    private static final String STRING_ADD_NEW_USER="Yeni kullanici ekle";
    public static final String KEY_USER_DATA="USER_DATA";
    public static final int REQUEST_CODE=0;
    private HashMap<String, UserData> userDatas;
    private TextView userDataTextView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.usersSpinner = (Spinner) findViewById(R.id.usersSpinner);
        this.usersSpinnerAdapter = new ArrayAdapter<String>(this.getApplicationContext(), android.R.layout.simple_spinner_item);
        this.usersSpinner.setAdapter(this.usersSpinnerAdapter);
        this.usersSpinnerAdapter.add(Hesapci2.STRING_ADD_NEW_USER);
        this.usersSpinner.setOnItemSelectedListener(new UsersSpinnerOnItemSelectedListener());
        this.userDatas = new HashMap<String, UserData>();
        this.userDataTextView = (TextView) findViewById(R.id.userDataTextView);
    }

    public void onActivityResult(int requestedCode, int returnedCode, Intent data) {
        super.onActivityResult(requestedCode, returnedCode, data);
        if (data.getExtras()!=null) {
            UserData newUser = (UserData) data.getExtras().getSerializable(Hesapci2.KEY_USER_DATA);
            if (!this.userDatas.containsKey(newUser.getName())) {
                this.userDatas.put(newUser.getName(), newUser);
                this.usersSpinnerAdapter.add(newUser.getName());
                this.usersSpinner.setSelection(this.usersSpinnerAdapter.getPosition(newUser.getName()), true);
                resetUserDataTextView();
            }
        }
        else {
            // TODO
        }
    }

    private void resetUserDataTextView() {
        UserData current = this.userDatas.get(this.usersSpinner.getSelectedItem().toString());
        this.userDataTextView.setText("Name="+current.getName()+", Resp="+current.getDateReachedResponsibility().toGMTString()+
                                        ", Pray="+current.getDateStartedPrayer().toGMTString()+
                                        ", Fast="+current.getDateStartedFasting().toGMTString());
    }

    public class UsersSpinnerOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
            if (parent.getSelectedItem().toString()==Hesapci2.STRING_ADD_NEW_USER) {
                Intent newUserIntent = new Intent(Hesapci2.this, NewUserActivity.class);
                Hesapci2.this.startActivityForResult(newUserIntent, Hesapci2.REQUEST_CODE);
            }
        }

        public void onNothingSelected(AdapterView<?> parent) {

        }
    }
}
